﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;

namespace AlertSystem.Business
{
    public partial class Warning_Method
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<Warning_Method> _instance;
        public static List<Warning_Method> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.Warning_Methods.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.Warning_Methods.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region static method
        public static Warning_Method GetByID(int Id)
        {
            try
            {
                return GetInstance().Find(p => p.Method_Id == Id);
            }
            catch
            {
                return null;
            }
        }

        public static Warning_Method FindItem(Predicate<Warning_Method> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static IEnumerable<Warning_Method> FindItems(Func<Warning_Method, bool> predicate)
        {
            try
            {
                return GetInstance().Where(predicate);
            }
            catch
            {
                return Enumerable.Empty<Warning_Method>();
            }
        }

        public static bool Any(Func<Warning_Method, bool> predicate)
        {
            try
            {
                return GetInstance().Any(predicate);
            }
            catch
            {
                return false;
            }
        }

        public static bool All(Func<Warning_Method, bool> predicate)
        {
            try
            {
                return GetInstance().All(predicate);
            }
            catch
            {
                return false;
            }
        }

        public static List<Warning_Method> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string method_name, string method_type, string type_of_object, string method_config, string description)
        {
            var resutl = context.Warning_Methods.AsQueryable();
            if (!string.IsNullOrWhiteSpace(method_name))
            {
                resutl = resutl.Where(p => p.Method_Name.Contains(method_name));
            }
            if (!string.IsNullOrWhiteSpace(method_type))
            {
                resutl = resutl.Where(p => p.Method_Type.Contains(method_type));
            }
            if (!string.IsNullOrWhiteSpace(type_of_object))
            {
                resutl = resutl.Where(p => p.Type_Of_Object.Contains(type_of_object));
            }
            if (!string.IsNullOrWhiteSpace(method_config))
            {
                resutl = resutl.Where(p => p.Method_Config.Contains(method_config));
            }
            if (!string.IsNullOrWhiteSpace(description))
            {
                resutl = resutl.Where(p => p.Description.Contains(description));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }

        public static string GetNameByID(DBDataContext context, int id)
        {
             Warning_Method obj = context.Warning_Methods.FirstOrDefault(p => p.Method_Id == id);
             if (obj != null) return obj.Method_Name;
             return "";
        }
        #endregion static method

        public static Warning_Method GetByObjectType(string objectType)
        {
            try
            {
                return GetInstance().Find(p => p.Type_Of_Object == objectType);
            }
            catch
            {
                return null;
            }
        }
        
        public void Save()
        {
            using (var context = new DBDataContext(AppSetting.ConnectionString))
            {
                if (Method_Id == 0)//add
                {
                    context.Warning_Methods.InsertOnSubmit(this);
                }
                else //edit
                {
                    var editobj = context.Warning_Methods.FirstOrDefault(p => p.Method_Id == this.Method_Id);
                    if (editobj != null)
                        ObjectClass.AssignValue<Warning_Method>(ref editobj, this);
                }
                context.SubmitChanges();
            }
        }
    }
}
